<?php

namespace app\admin\controller\info;

use app\admin\controller\Base;
use app\common\info\ArticleCategoryModel;

class ArticleCategory extends Base
{
    /**
     * 列表
     */
    public function list()
    {
        $params = request()->only(['page', 'limit', 'name']);

        if (isset($params['name']) && !empty($params['name'])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }

        $list = (new ArticleCategoryModel)->where($where ?? [])
            ->order('sort', 'DESC')
            ->paginate([
                'list_rows' => $params['limit'] ?? 10,
                'page' => $params['page'] ?? 1
            ]);

        return response_success('', [
            'count' => $list->total(),
            'data' => $list->items()
        ]);
    }

    /**
     * 添加
     */
    public function add()
    {
        $params = request()->only(['name', 'sort']);
        $this->validate($params, [
            'name|名称' => 'require',
            'sort|排序值' => 'number'
        ]);

        if ((new ArticleCategoryModel)->where('name', $params['name'])->find()) {
            return response_error('分类名称已存在');
        }

        (new ArticleCategoryModel)->insert([
            'name' => $params['name'],
            'sort' => $params['sort'],
            'admin_id' => $this->adminInfo['id'],
            'create_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

    /**
     * 删除
     */
    public function del()
    {
        $params = request()->only(['ids']);
        $this->validate($params, [
            'ids' => 'require'
        ], [
            'ids.require' => '请选择删除分类'
        ]);

        $articleCategoryModel = new ArticleCategoryModel;
        if (!$articleCategoryModel->delCategory($params['ids'])) {
            return response_error($articleCategoryModel->getError());
        }

        return response_success();
    }

    /**
     * 编辑
     */
    public function edit()
    {
        $params = request()->only(['id', 'name', 'sort']);
        $this->validate($params, [
            'id|ID' => 'require',
            'name|名称' => 'require',
            'sort|排序值' => 'number'
        ]);

        if ((new ArticleCategoryModel)->where('id', '<>', $params['id'])->where('name', $params['name'])->find()) {
            return response_error('分类名称已存在');
        }

        (new ArticleCategoryModel)->where('id', $params['id'])->update([
            'name' => $params['name'],
            'sort' => $params['sort'],
            'update_time' => date('Y-m-d H:i:s')
        ]);

        return response_success();
    }

    /**
     * 获取所有分类
     */
    public function getAll()
    {
        $params = request()->only(['name']);

        if (isset($params['name']) && !empty($params['name'])) {
            $where[] = ['name', 'like', "%{$params['name']}%"];
        }

        $list = (new ArticleCategoryModel)->where($where ?? [])
            ->field('id,name')
            ->order('sort', 'DESC')
            ->select()->toArray();

        return response_success('', $list);
    }

}